package com.warmlight.voicepacket.db;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.util.Log;

import com.warmlight.voicepacket.data.FloatPlayListData;
import com.warmlight.voicepacket.data.FloatPlayListDetailData;
import com.warmlight.voicepacket.data.RecommendFeed;

import java.util.ArrayList;
import java.util.List;

import static com.warmlight.voicepacket.db.VoicePacketDBHelper.AUDIO_COVER_ID;
import static com.warmlight.voicepacket.db.VoicePacketDBHelper.AUDIO_ID;
import static com.warmlight.voicepacket.db.VoicePacketDBHelper.AUDIO_PS;
import static com.warmlight.voicepacket.db.VoicePacketDBHelper.AUDIO_TITLE;
import static com.warmlight.voicepacket.db.VoicePacketDBHelper.AUDIO_URL;
import static com.warmlight.voicepacket.db.VoicePacketDBHelper.LIST_AUDIO;
import static com.warmlight.voicepacket.db.VoicePacketDBHelper.LIST_COUNT;
import static com.warmlight.voicepacket.db.VoicePacketDBHelper.LIST_ID;
import static com.warmlight.voicepacket.db.VoicePacketDBHelper.LIST_IMG_PATH;
import static com.warmlight.voicepacket.db.VoicePacketDBHelper.LIST_NAME;
import static com.warmlight.voicepacket.db.VoicePacketDBHelper.LIST_PS;
import static com.warmlight.voicepacket.db.VoicePacketDBHelper._ID;
import static com.warmlight.voicepacket.db.VoicePacketProvider.URI_TABLE_AUDIO;
import static com.warmlight.voicepacket.db.VoicePacketProvider.URI_TABLE_LIST;

/**
 * Created by jason on 2018/5/28.
 * 注明，目前用的list的id都是_id字段，audio的id都是audio_id字段
 */

public class DBHelper {
    private static DBHelper mInstance;
    private static Context mContext;
    private ContentResolver mResolver;

    //播放列表数据列
    private String listColumn[] = {_ID,LIST_ID,LIST_NAME,LIST_IMG_PATH,LIST_COUNT,LIST_AUDIO,LIST_PS};
    //音频数据列
    private String audioColumn[] = {_ID,AUDIO_ID,AUDIO_TITLE,AUDIO_URL,AUDIO_COVER_ID,AUDIO_PS};

    private DBHelper(){
        mResolver = mContext == null ? null :mContext.getContentResolver();
    }
    public static DBHelper getInstance(Context context) {
        if(mInstance == null){
            synchronized (DBHelper.class){
                if(mInstance == null){
                    mContext = context.getApplicationContext();
                    mInstance = new DBHelper();
                }
            }
        }
        return mInstance;
    }

    //查询所有我的播放列表信息
    public List<FloatPlayListData> queryAllList(){
        Cursor cursor = mResolver.query(URI_TABLE_LIST,listColumn,null,null,null);
        List<FloatPlayListData> dataList = new ArrayList<>();
        if(cursor != null){
            if(cursor.moveToFirst()){
                do {
                    dataList.add(new FloatPlayListData(cursor.getString(0),cursor.getString(1),cursor.getString(2),
                            cursor.getString(3),cursor.getString(4),cursor.getString(5),cursor.getString(6)));

                } while (cursor.moveToNext());
            }
        }
        cursor.close();
        return dataList;
    }

    //查询所有音频信息
    public List<FloatPlayListDetailData> queryAllAudio(){
        Cursor cursor = mResolver.query(URI_TABLE_AUDIO,audioColumn,null,null,null);
        List<FloatPlayListDetailData> dataList = new ArrayList<>();
        if(cursor != null){
            if(cursor.moveToFirst()){
                do {
                    dataList.add(new FloatPlayListDetailData(cursor.getString(0),cursor.getString(1),cursor.getString(2),
                            cursor.getString(3),cursor.getString(4),cursor.getString(5)));

                } while (cursor.moveToNext());
            }
        }
        cursor.close();
        return dataList;
    }

    // 根据id查询audio表中数据
    public FloatPlayListDetailData queryAudioById(@NonNull String id){
        FloatPlayListDetailData data = null;

        Uri uri = Uri.parse(URI_TABLE_AUDIO+"");

        Cursor cursor = mResolver.query(uri, audioColumn, AUDIO_ID+" = ?", new String[]{id}, null);
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                data = new FloatPlayListDetailData(cursor.getString(0),cursor.getString(1),cursor.getString(2),
                        cursor.getString(3),cursor.getString(4),cursor.getString(5));
            }
        }
        cursor.close();
        return data;
    }

    // 根据播放列表id，查询列表中所有音频bean
    public List<FloatPlayListDetailData> queryAudiosByListId(@NonNull String listId){
        List<FloatPlayListDetailData> audioList = new ArrayList<>();
        FloatPlayListData listData = queryListDataByID(listId);
        if(listData==null){
            Log.i("DBHelper","未根据播单id查询到该播单");
            return null;
        }

        String[] audioIds = listData.getList_audio().split(",");

        for(String audioId : audioIds){
            FloatPlayListDetailData audioData = queryAudioById(audioId);
            if(audioData!=null){
                audioList.add(audioData);
            }
        }

        return audioList;
    }

    //根据播放列表_id，查询playlist
    public FloatPlayListData queryListDataByID(@NonNull String id){

        FloatPlayListData data = null;

            Uri uri = Uri.parse(URI_TABLE_LIST+"");

            Cursor cursor = mResolver.query(uri, listColumn, _ID+" = ?", new String[]{id}, null);
            if (cursor != null) {
                if (cursor.moveToFirst()) {
                    data = new FloatPlayListData(cursor.getString(0),cursor.getString(1),cursor.getString(2),
                        cursor.getString(3),cursor.getString(4),cursor.getString(5),cursor.getString(6));
                }
            }
            cursor.close();
        return data;
    }

    //TODO 根据播放列表存储的audio的ids，查询音频bean的list
    public List<FloatPlayListDetailData> queryListData(@NonNull String audios){

        List<FloatPlayListDetailData> dataList = new ArrayList<>();
        String[] ids = audios.split(",");

        for(String id : ids){
            Uri uri = Uri.parse(URI_TABLE_AUDIO + "/" + id);
            Cursor cursor = mResolver.query(uri, audioColumn, null, null, null);
            if (cursor != null) {
                if (cursor.moveToFirst()) {
                    dataList.add(new FloatPlayListDetailData(cursor.getString(0),cursor.getString(1),cursor.getString(2),
                        cursor.getString(3),cursor.getString(4),cursor.getString(5)));
                }
            }
            cursor.close();
        }
        return dataList;
    }

    //删除本地播放列表
    public void delPlayList(@NonNull String _id){
        Uri uri = Uri.parse(URI_TABLE_LIST+"");
        int delete = mResolver.delete(uri, "_id = ?", new String[]{_id});
//        Log.e("TAG","delete:"+delete);
    }

    //TODO 删除本地audio
    public void delAudios(@NonNull String audioId){
        Uri uri = Uri.parse(URI_TABLE_AUDIO+"");
        int delete = mResolver.delete(uri, "_id = ?", new String[]{audioId});
//        Log.e("TAG","delete:"+delete);
    }

    //添加本地播放列表
    public void addList(@NonNull FloatPlayListData data){
        ContentValues cv = new ContentValues();
        cv.put(LIST_ID, data.getList_id());
        cv.put(LIST_NAME, data.getList_name());
        cv.put(LIST_IMG_PATH, data.getList_img_path());
        cv.put(LIST_COUNT, data.getList_count());
        cv.put(LIST_AUDIO, data.getList_audio());
        cv.put(LIST_PS, data.getList_ps());
        mResolver.insert(URI_TABLE_LIST, cv);
    }

    //TODO 添加audio到table
    public void addAudios(@NonNull List<RecommendFeed.DataBean.ListBean> dataList){
        for(RecommendFeed.DataBean.ListBean data : dataList){
            ContentValues cv = new ContentValues();
            cv.put(AUDIO_ID, data.getId());
            cv.put(AUDIO_TITLE, data.getTitle());
            cv.put(AUDIO_URL, data.getUrl());
            cv.put(AUDIO_COVER_ID, data.getCover_id());
            cv.put(AUDIO_PS, data.getCover_pic());//以后扩展字段
            mResolver.insert(URI_TABLE_AUDIO, cv);
        }
    }
//    public void addAudios(@NonNull List<FloatPlayListDetailData> dataList){
//        for(FloatPlayListDetailData data : dataList){
//            ContentValues cv = new ContentValues();
//            cv.put(AUDIO_ID, data.getAudio_id());
//            cv.put(AUDIO_TITLE, data.getAudio_title());
//            cv.put(AUDIO_URL, data.getAudio_url());
//            cv.put(AUDIO_COVER_ID, data.getAudio_cover_id());
//            cv.put(AUDIO_PS, data.getAudio_ps());
//            mResolver.insert(URI_TABLE_AUDIO, cv);
//        }
//    }

    //TODO 更新本地播放列表
//    public void updateList(@NonNull FloatPlayListData data){
//        ContentValues cv = new ContentValues();
//
//        cv.put(_ID, data.get_id());
//        cv.put(LIST_ID, data.getList_id());
//        cv.put(LIST_NAME, data.getList_name());
//        cv.put(LIST_IMG_PATH, data.getList_img_path());
//        cv.put(LIST_COUNT, data.getList_count());
//        cv.put(LIST_AUDIO, data.getList_audio());
//        cv.put(LIST_PS, data.getList_ps());
//        Uri uri = Uri.parse(URI_TABLE_LIST+"");
//        mResolver.update(uri, cv, null, null);
//    }

    //更新audiosTable
    //TODO

}
